package sg.edu.nus.iss.pacman.form;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import sg.edu.nus.iss.pacman.manager.socket.SocketClientManager;
import sg.edu.nus.iss.pacman.midlet.PacManMIDlet;

/**
 * This is screen to Download Maze
 * 
 * @author Daniel
 *
 */
public class DownloadMazeForm extends Form implements CommandListener {

    private static final String TITLE = "Download maze from Server";
    private PacManMIDlet mainApp;
    private String mazename;
    private Command cmdBack;

    /**
     * Constructor for DoanloadMazeForm
     *
     * @param mainApp PacManMIDlet
     */
    public DownloadMazeForm(PacManMIDlet mainApp, String mazename) {
        super(TITLE);
        this.mazename = mazename;
        String message = "Downloading " + mazename + " from server. \n. Please wait....";
        TextField messageField = new TextField("", message, message.length(), TextField.UNEDITABLE);
        this.mainApp = mainApp;
        init();
    }

    /**
     * Initializes the screen
     */
    private void init() {
        cmdBack = new Command(PacManMIDlet.LABEL_CMD_BACK, Command.EXIT, 1);
        addCommand(cmdBack);
        setCommandListener(this);
        SocketClientManager manager=new SocketClientManager(mainApp.getServerURL(), mainApp);
        manager.downloadMaze(mazename);
    }

    /**
     * This is a method which handles command actions
     */
    public void commandAction(Command cmd, Displayable disp) {
        if (cmd == cmdBack) {
            mainApp.displayMainMenu();
        }
    }
}
